{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 高斯符号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Math.floor 和 Math.ceil 是工程中常用的两个函数，它在数学上有专门的符号表示。分别是取底符($\\lfloor x\\rfloor$,floor)，和取顶符($\\lceil x \\rceil$, ceil)。floor 有地板的意思，ceil 有天花板的意思，可以帮助记忆。\n",
    "\n",
    "其中向下取整 $\\lfloor x\\rfloor$, 还有个名字叫 *高斯符号* ，也写为 $[x]$，注意是左右括号，如果是双竖线则是[绝对值](https://math.haozi.me/abs.html) 。\n",
    "\n",
    "如果数学公式中提到高斯符号或者出现 $[x]$，只表示下取整(floor)。\n",
    "高斯符号在很多语言中用 int 函数表示，比如 Python 中 `int(2.99) = 2`\n",
    "\n",
    "下文主要讲高斯符号，ceil 的功能与它相近，但是向上取整。\n",
    "\n",
    "当 x 小数位不为 0 时，`ceil(x) = floor(x) + 1`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 举例来说\n",
    "\n",
    "$[3.633]=3$, $[56]=56$, $[-2]=-2$, $[\\pi] = 3$, $[-2.197] = -3$\n",
    "\n",
    "$[0.999] = 0$\n",
    "\n",
    "$[0.999\\cdots] = 1$,\n",
    "\n",
    "其中要注意的是，负数的 floor 向下取整，要往实数轴的左边取临近的整数。\n",
    "眼尖的同学发现 0 后面无数个 9 为什么高斯函数是 1，因为 [$0.999\\cdots$ 就是 1 啊](https://zh.wikipedia.org/wiki/0.999%E2%80%A6)(笑)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 高斯符号运算法则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* $[x]\\leq x<[x]+1$ (当且仅当 x 是整数时，左边的等号成立)\n",
    "* 等幂运算：$[[x]]=[x]$\n",
    "* 对任意的整数k和任意实数x，\n",
    "$[{k+x}]=k+[x]$\n",
    "* 对于所有实数x，有：\n",
    "\n",
    "$\\left[{\\frac {x}{2}}\\right]={\\frac {1}{4}}((-1)^{[x]}-1+2[x])$\n",
    "\n",
    "${\\displaystyle \\left[{\\frac {x}{3}}\\right]={\\frac {-2}{\\sqrt {3}}}\\sin({\\frac {2\\pi }{3}}[x]+{\\frac {\\pi }{3}})+1}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JS 实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```javascript\n",
    "const floor = (x) => {\n",
    "  if (x >= 0) return Math.trunc(x)\n",
    "  return Math.trunc(x) - 1\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里用了 [trunc](https://math.haozi.me/trunc.html) 这个还没有提到的函数，这个方法就是无脑将数字的小数部分去掉，只保留整数部分。没太多好讲的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Math.round "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "中文翻译成四舍五入，其实不准确，[参见](https://math.haozi.me/round.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你觉得高斯符号以下向上取整有哪些实际作用呢？欢迎[留言](https://github.com/haozi/Math/issues/13)。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
